package com.hngy.xpq.adapterdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.hngy.xpq.adapterdemo.adapter.GridAdapter;
import com.hngy.xpq.adapterdemo.adapter.HorizontalAdapter;
import com.hngy.xpq.adapterdemo.adapter.ListAdapter;
import com.hngy.xpq.adapterdemo.bean.Data;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView r1, r2;
    Button qh, xf_btn;
    boolean index = true;
    List<Data> h;
    List<Data> dataList;    // 获取到具体生成数据的地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 绑定控件
        onclick();
        // 获取数据，这里就不连接服务器接收数据了，主要还是为了copy的初学者能运行成功
        dataList = generateData();
        h = HorizontalData();
        // 初始化布局
        init();
    }

    private void onclick() {
        r1 = findViewById(R.id.r1);
        r2 = findViewById(R.id.r2);
        qh = findViewById(R.id.qh);
        xf_btn = findViewById(R.id.xf_btn);
        // 切换布局
        qh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTheme(R.style.AppThemeRed);
                if (index) {
                    getGridAdapter();
                } else {
                    getListAdapter();
                }
                index = !index;
            }
        });
        r1.setOnScrollListener(new RecyclerView.OnScrollListener() {
            /**
             * Callback method to be invoked when the RecyclerView has been scrolled. This will be
             * called after the scroll has completed.
             * <p>
             * This callback will also be called if visible item range changes after a layout
             * calculation. In that case, dx and dy will be 0.
             *
             * @param recyclerView The RecyclerView which scrolled.
             * @param dx           The amount of horizontal scroll.
             * @param dy           The amount of vertical scroll.
             */
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    xf_btn.setVisibility(View.GONE);                     // 隐藏添加按钮
                } else {
                    xf_btn.setVisibility(View.VISIBLE);
                }
            }
        });
        r2.setOnScrollListener(new RecyclerView.OnScrollListener() {
            /**
             * Callback method to be invoked when the RecyclerView has been scrolled. This will be
             * called after the scroll has completed.
             * <p>
             * This callback will also be called if visible item range changes after a layout
             * calculation. In that case, dx and dy will be 0.
             *
             * @param recyclerView The RecyclerView which scrolled.
             * @param dx           The amount of horizontal scroll.
             * @param dy           The amount of vertical scroll.
             */
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    xf_btn.setVisibility(View.GONE);                     // 隐藏添加按钮
                } else {
                    xf_btn.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private List<Data> generateData() {
        // 具体生成的数据
        List<Data> temp = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            Data data = new Data();
            data.setImage(R.drawable.ic_launcher_background);
            data.setName("item-" + i);
            temp.add(data);
        }
        return temp;
    }

    private List<Data> HorizontalData() {
        // 具体生成的数据
        List<Data> temp = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Data data = new Data();
            data.setImage(R.drawable.ic_launcher_foreground);
            data.setName("操作-" + i);
            temp.add(data);
        }
        return temp;
    }

    private void init() {
        // 生成布局
        getListAdapter();
        getHorizontalAdapter();
    }

    // Horizontal适配器
    private void getHorizontalAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        r1.setLayoutManager(layoutManager);
        HorizontalAdapter adapter = new HorizontalAdapter(h);
        r1.setAdapter(adapter);
    }

    // List适配器
    private void getListAdapter() {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        r2.setLayoutManager(layoutManager);
        ListAdapter adapter = new ListAdapter(dataList);
        r2.setAdapter(adapter);
    }

    // grid布局
    private void getGridAdapter() {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        r2.setLayoutManager(layoutManager);
        GridAdapter adapter = new GridAdapter(dataList);
        r2.setAdapter(adapter);
    }

}